<script>
import { updateMajor } from '@/api/admin/major.js'
import majorRules from '@/rules/majorRules.js'
import { selectDepartmentOptions } from '@/api/authApi.js'
export default {
  props: ['modelValue'],
  emits: ['update:modelValue', 'resetData'],
  computed: {
    flag: {
      get() {
        return this.modelValue
      },
      set(nv) {
        this.$emit('update:modelValue', nv)
      },
    },
  },
  data() {
    return {
      departments: [],
      params: {
        department_id: '',
      },
      majorRules,
      majorData: {
        major_name: '',
        major_department_id: '',
        major_id: '',
      },
    }
  },

  methods: {
    async initDepartmentOptions() {
      this.departments = await selectDepartmentOptions(this.params)
    },
    remoteMethod(key) {
      this.params.department_id = key
      this.initDepartmentOptions()
    },
    async EditNewMajor() {
      await this.$refs.form.validate()
      await updateMajor(this.majorData)
      this.flag = false
      this.$emit('resetData')
    },
    resetFormData() {
      this.majorData = {}
      this.$refs.form.resetFields()
    },
    setDefaultForm(obj) {
      obj.major_id = obj.major_id
      obj.major_name = obj.major_name
      obj.major_department_id = obj.department_id
      this.majorData = obj
      console.log(obj)
    },
  },
  created() {
    this.initDepartmentOptions()
  },
}
</script>

<template>
  <el-dialog v-model="flag" title="专业信息更新" width="600px" @closed="resetFormData">
    <el-form label-width="80px" :inline="false" ref="form" :model="majorData" :rules="majorRules">
      <el-form-item label="专业编号" prop="major_id">
        <el-input type="text" v-model="majorData.major_id"></el-input>
      </el-form-item>
      <el-form-item label="专业名称" prop="major_name">
        <el-input type="text" v-model="majorData.major_name"></el-input>
      </el-form-item>
      <el-form-item label="院系" prop="major_department_id">
        <el-select
          clearable
          v-model="majorData.major_department_id"
          filterable
          remote
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="d in departments"
            :key="d.department_id"
            :value="d.department_id"
            :label="d.department_name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="$emit('update:modelValue', nv)">取消</el-button>
      <el-button type="primary" @click="EditNewMajor()">更新</el-button>
    </template>
  </el-dialog>
</template>
